{-# LANGUAGE DeriveDataTypeable         #-}
{-# OPTIONS -Wall #-}

-----------------------------------------------------------------------------
-- |
-- Module      :  DirectoryScript.Lib.Base
-- Copyright   :  (c) Stephen Peter Tetley 2012
-- License     :  BSD3
--
-- Maintainer  :  stephen.tetley@gmail.com
-- Stability   :  experimental
-- Portability :  to be determined
--
-- Base objects and utils.
--
-- Note - should probably distinguish between Windows and Unix, 
-- as we work from listings (rather than interactively through IO) 
-- at some point we could have both styles.
--
-----------------------------------------------------------------------------


module DirectoryScript.Lib.Base
  (
    Atom
  , atoms

  , DateTime(..)
  , invalid_date_time
  , ppDateTime


  ) where

import Text.ZFormat hiding ( null )     -- package: zformat

import Data.Generics hiding ( empty )   -- package: syb


type Atom = String

atoms :: FilePath -> [Atom]
atoms [] = []
atoms xs = case pivot (/= '\\') xs of
             Left as -> if null as then [] else [as]
             Right (a,_,as) -> a : atoms as

pivot :: (a -> Bool) -> [a] -> Either [a] ([a],a,[a])
pivot test xs = let (ys,zs) = span test xs in case zs of
                  (a:as) -> Right (ys,a,as)
                  []     -> Left ys
                       




data DateTime = DateTime
    { dt_year   :: !Int 
    , dt_month  :: !Int 
    , dt_day    :: !Int
    , dt_hour   :: !Int
    , dt_min    :: !Int
    }
  deriving (Eq,Ord,Show,Data,Typeable)


invalid_date_time :: DateTime
invalid_date_time = DateTime    
    { dt_year   = 1900
    , dt_month  = 1
    , dt_day    = 1
    , dt_hour   = 0
    , dt_min    = 0
    }


-- | Printed as per Windows directory listings:
-- 
-- > dd/mm/yyyy hh:mm
--
ppDateTime :: DateTime -> Doc
ppDateTime a = year <+> day
  where
    year = punctuate (char '/') [ nat2 $ dt_day a
                                , nat2 $ dt_month a
                                , nat4 $ dt_year a ]
    day  = nat2 (dt_hour a) <> colon <> nat2 (dt_min a)



--------------------------------------------------------------------------------
-- ZFormat changes

